<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Qiniu\Auth;
use Qiniu\Processing\PersistentFop;

class VideoSpliceController extends AdminbaseController{

    private $accessKey = '';
    private $secretKey = '';
    private $bucket = '';
    private $bucket_url = '';

    public function _initialize() {
        //继承初始化方法
        parent::_initialize ();
        //设置控制器默认模型
        $this->Model = D('QiniuVideo');
        $this->accessKey = '3OSMK1sA7gNKXQRcVhAP7L_FB1MsTccsEXlzklG6';
        $this->secretKey = '3mNe0pIJemfaqy6KcwV-oQG06I10AdcN44-OW69l';
        $this->bucket = 'wangyh';
        $this->bucket_url = 'http://7xr6vu.com1.z0.glb.clouddn.com/';
    }
	
	// 视频上传操作
	public function add() {
        $this->assign('domain',$this->bucket_url);
        $this->display();
    }

    // 视频列表页面
    public function index() {
        $videos=$this->Model->order('id desc')->select();
        $this->assign("videos",$videos);
        $this->assign("domain", $this->bucket_url);
        $this->display();
    }

    // 友情链接删除
    public function delete(){
        if(isset($_POST['ids'])){

        }else{
            $id = I("get.id",0,'intval');
            if ($this->Model->delete($id)!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }

    }

    public function fops() {
        $key = I('get.key');
        $is_water = I('get.is_water');
        $water_type = I('get.water_type');
        $is_start = I('get.is_start');
        $is_cut = I('get.is_cut');
        $width = I('get.width');
        $height = I('get.height');
        $is_async = I('get.is_async');
        $auth = new Auth($this->accessKey, $this->secretKey);
        //要转码的文件所在的空间和文件名
        $bucket = $this->bucket;
        //处理命令
        $shells = '';
        //转码是使用的队列名称
        $pipeline = 'wangyh_net';
        $notify_url = 'http://julaijin.cc/index.php?m=Admin&c=Public&a=api_qiniu_callback';
        $pfop = new PersistentFop($auth, $bucket, $pipeline, $notify_url);
        $water_add = function ($fops, $type=1) {
            $res_str = '';
            $type = intval($type);
            switch ($type) {
                case 1:
                    $water_url = \Qiniu\base64_urlSafeEncode('http://static.shenyou.tv/video/xiaowangzi.png');
                    $res_str = $fops . '|avthumb/mp4/wmImage/' . $water_url . '/wmGravity/NorthWest/wmOffsetX/30/wmOffsetY/20';
                    break;
                case 2:
                    $water_url = \Qiniu\base64_urlSafeEncode('http://ww3.sinaimg.cn/large/6de47e09jw1f9342m7rq3j206601fa9x.jpg');
                    $res_str = $fops . '|avthumb/mp4/wmImage/' . $water_url . '/wmGravity/NorthEast/wmOffsetX/-30/wmOffsetY/20';
                    break;
                case 3:
                    $water_url = \Qiniu\base64_urlSafeEncode('http://7xr6vu.com1.z0.glb.clouddn.com//logo/kanzongyi.png');
                    $res_str = $fops . '|avthumb/mp4/wmImage/' . $water_url . '/wmGravity/NorthWest/wmOffsetX/30/wmOffsetY/20';
                    break;
                case 4:
                    $water_url = \Qiniu\base64_urlSafeEncode('http://ww2.sinaimg.cn/large/6de47e09gw1fbk16k3csij207e01o746.jpg');
                    $res_str = $fops . '|avthumb/mp4/wmImage/' . $water_url . '/wmGravity/NorthEast/wmOffsetX/-30/wmOffsetY/20';
		    break;
                default:
                    $water_url = \Qiniu\base64_urlSafeEncode('http://static.shenyou.tv/video/logo.png');
                    $res_str = $fops . '|avthumb/mp4/wmImage/' . $water_url . '/wmGravity/NorthWest/wmOffsetX/30/wmOffsetY/20';
            }
            return $res_str;
        };
        // 如果拼接片头片尾，处理对象为片头
        if($is_start) {
            $shells = $shells . '_start';
            $fops = 'avconcat/2/format/mp4';
            $video_start = 'video/pro_6_2.mp4';
            $video_end = 'video/tail_2.mp4';
            $fops = $fops . '/' .\Qiniu\base64_urlSafeEncode($this->bucket_url.$key).'/'.\Qiniu\base64_urlSafeEncode($this->bucket_url.$video_end);
            // 添加片头片尾不能跟视频裁剪一同操作
            if($is_water) {
                $fops = $water_add($fops, $water_type);
                $shells = $shells . '_water';
            }
            // 设置处理对象为当前Key
            $pro_key = $video_start;
        } else {
            $fops = "avthumb/mp4/s/1280x720/aspect/16:9";
            $shells = $shells . '_trans';
            if($is_cut) {
                $start = I('get.cut_start');
                $duration = I('get.cut_end') - $start;
                $fops = $fops . '/ss/'.$start.'/t/'.$duration;
                $shells = $shells . '_cut';
            }
            if($is_water) {
                $shells = $shells . '_water';
                $fops = $water_add($fops, $water_type);
            }
            // 设置处理对象为当前key
            $pro_key = $key;
        }
        // 另存为命令
        $new_key = substr($key, 0, stripos($key, '.')) . $shells .'.' . substr($key, stripos($key, '.')+1);
        $entry = \Qiniu\base64_urlSafeEncode("$bucket:$new_key");
        $fops = $fops."|saveas/$entry";
        ini_set('max_execution_time', 300);
        // 执行持久化处理
        list($id, $err) = $pfop->execute($pro_key, $fops);
        if ($err != null) {
            var_dump($err);
        } else {
            //echo "PersistentFop Id: $id\n";
        }
        // 组装数据库存储数据
        $data['origin_key'] = $key;
        $data['key'] = $new_key;
        $data['pro_shells'] = $fops;
        $data['thumb'] = $this->bucket_url . $new_key . '?vframe/jpg/offset/7/w/480/h/270/';
        $data['info'] = $width . 'x' .$height;
        $data['status'] = 'processing';
        $data['userid'] = $this->UserInfo['id'];
        $data['persistentid'] = $id;
        $data = $this->Model->create($data);
        if($data) {
            $new_id = $this->Model->add($data);
            if($new_id){
                //action_log('Add_VideoSlice', 'VideoSlice', $new_id);
            }
        }

        if($is_async) {
            $this->ajaxReturn(array(
                'url' => $this->bucket_url . $new_key,
                'key' => $new_key,
            ));
        } else {
            $this->ajaxReturn(array(
                'id' => $id,
            ));
        }
    }

    public function upToken() {
        $auth = new Auth($this->accessKey, $this->secretKey);
        $upToken = $auth->uploadToken($this->bucket);
        $data = array('uptoken' => $upToken);
        $this->ajaxReturn($data);
    }

    public function status() {
        $persistent_id = I('get.id');
        $res = array('status' => 'processing');
        if($persistent_id) {
            $_info = $this->Model->where(array('persistentid'=>$persistent_id))->find();
            if($_info['status'] == 'success') {
                $res['status'] = 'success';
                $res['url'] = $this->bucket_url . $_info['key'];
                $res['key'] = $_info['key'];
            }
            if ($_info['status'] == 'error') {
                $res['status'] = 'error';
            }
        }
        $this->ajaxReturn($res);
    }
	
}
